<template>
  <div>
    <!-- 搜索页面头部 -->
    <div class="search-header">
      <!-- 后退按钮 -->
      <van-icon name="arrow-left" color="white" size="0.48rem" class="goback" @click="$router.back()"/>
      <!-- 搜索组件 -->
      <van-search  @search='searchEV()' v-model.trim="kw" @input='inputFn' focus placeholder="请输入搜索关键词" background="#007BFF" shape="round" />
    </div>
    <!-- 搜索建议列表 -->
<div  v-if="kw.length !== 0"  class="sugg-list">
    <div @click="searchEV(option)" v-for="(option,index) in options" :key="index" class="sugg-item" v-html="lightFn(option,kw) " >    </div>
</div>
<div v-else class="search-history">
    <!-- 标题 -->
    <van-cell title="搜索历史">
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
          <template #right-icon>
<van-icon @click="clearHistory" name="delete" class="search-icon" />
 </template>
    </van-cell>

    <!-- 历史列表 -->
    <div  class="history-list">
        <span @click="searchEV(val)"  v-for="(val,index) in history" :key="index" class="history-item">{{val}}</span>
    </div>
</div>
  </div>
</template>

<script>
import { setHistory, getHistory, clearHistory } from '@/utils/history'
import { getSearchAdvice } from '@/api'
export default {
  name: 'SearchCom',
  data () {
    return {
      kw: '', // 搜索关键字
      timer: null,
      options: [] // 联想建议列表
    }
  },
  methods: {
    // 防抖
    inputFn () {
      clearTimeout(this.timer)
      if (this.kw.length === 0) {
        this.options = []
      } else {
        this.timer = setTimeout(async () => {
          if (this.kw.length === 0) { return }
          const result = await getSearchAdvice(this.kw)
          this.options = result.data.data.options
        }, 300)
      }
    },
    // 字符串高亮
    lightFn (originStr, target) {
      if (originStr) {
        // originStr：原来字符串，target：目标字符串
        const reg = new RegExp(target, 'ig')
        return originStr.replace(reg, (match) => { return `<span style='color:red;'>${target}</span>` })
      }
    },
    // 搜索确认跳转
    searchEV (val) {
      const History = this.history.slice(0)
      if (val) {
        History.unshift(val)
        setHistory(History)
        this.$router.push({ path: `/searchresult/${val}` })
      } else {
        History.unshift(this.kw)
        setHistory(History)
        this.$router.push({ path: `/searchresult/${this.kw}` })
      }
    },
    // 清空搜索记录
    clearHistory () {
      clearHistory()
    }
  },
  computed: {
    // 搜索历史
    history () {
      return getHistory() || []
    }
  }
}
</script>

<style scoped lang="less">
.search-header {
  height: 46px;
  display: flex;
  align-items: center;
  background-color: #007bff;
  overflow: hidden;
  /*后退按钮*/
  .goback {
    padding-left: 14px;
  }
  /*搜索组件*/
  .van-search {
    flex: 1;
  }
}
.sugg-list {
  .sugg-item {
    padding: 0 15px;
    border-bottom: 1px solid #f8f8f8;
    font-size: 14px;
    line-height: 50px;
    // 实现省略号的三行代码
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.search-icon {
  font-size: 16px;
  line-height: inherit;
}

.history-list {
  padding: 0 10px;
  .history-item {
    display: inline-block;
    font-size: 12px;
    padding: 8px 14px;
    background-color: #efefef;
    margin: 10px 8px 0px 8px;
    border-radius: 10px;
  }
}
</style>
